/**
 * @author        Nicolas Bottarini <nicolasbottarini@gmail.com>, 404 <http://www.proyecto404.com>
 * @version       $Rev: 3 $
 * @lastrevision  $Date: 2011-09-12 16:45:37 +0000 (Mon, 12 Sep 2011) $	 
 */
package com.proyecto404.core.configuration {
	import com.proyecto404.core.TranslationManager;
	import com.proyecto404.core.collections.Dictionary;
	import com.proyecto404.core.collections.IIterator;
	import com.proyecto404.core.collections.List;
	import com.proyecto404.core.exception.InvalidOperationException;
	import com.proyecto404.core.util.StringHelper;

	public class TranslationsConfigurationHandler extends ConfigurationHandler {
		private var _translations:Dictionary;
		
		public function TranslationsConfigurationHandler(name:String) {
			super(name);
			_translations = new Dictionary();
		}
		
		public override function addConfigurations(xmlData:XML):void {
			for each(var node:XML in xmlData.children()) {
				var name:String = node.name().toString();
				switch(name) {
					case "key":
						processKey(node, "");
						break;
					case "group": 
						processGroup(node, "");
						break;
					default:
						throw new InvalidOperationException("Invalid node: " + name);
						break;
				}
			}
		}
		
		private function processGroup(group:XML, namespace:String = ""):void {
			var groupName:String = (group.@name).toString();
			if (groupName == "") {
				throw new InvalidOperationException("Group name cannot be empty")
			}
			if (namespace != "") {
				groupName = namespace + "." + groupName;
			}
			for each (var node:XML in group.children()) {
				var name:String = node.name().toString();
				switch(name) {
					case "key":
						processKey(node, groupName);
						break;
					case "group":
						processGroup(node, groupName);
						break;
					default:
						throw new InvalidOperationException("Invalid node: " + name);
						break;
				}
			} 
		}
		
		private function processKey(key:XML, namespace:String = ""):void {
			var id:String = (key.@id).toString();
			if (namespace != "") {
				id = namespace + "." + id;
			}
			for each (var languageNode:XML in key.children()) {
				var language:String = languageNode.name().toString(); 
				addTranslation(language, id, languageNode.text().toString()); 
			} 
		}
		
		private function addTranslation(language:String, id:String, text:String):void {
			if (!_translations.containsKey(language)) {
				_translations.add(language, new Dictionary());
			}
			if (Dictionary(_translations.get(language)).containsKey(id)) {
				throw new InvalidOperationException("Duplicate key with id: " + id); 
			}
			
			Dictionary(_translations.get(language)).add(id, text);
		}
		
		public override function get(configuration:String):* { 
			throw new InvalidOperationException();
		}
		
		public override function contains(configuration:String):Boolean {
			throw new InvalidOperationException();
		}
		
		public override function getConfigurations():IIterator {
			return _translations.getIterator();		
		}
	}
}